﻿package {
	import com.dncompute.canvas.BrowserCanvas;
	import com.mxprojects.gui.BreadCrumbs;
	import com.mxprojects.gui.Footer;
	import com.mxprojects.gui.Header;
	import com.mxprojects.gui.LoadingAnimation;
	import com.mxprojects.gui.PageNav;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.io.Config;
	import com.mxprojects.language.Word;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;

	public class MXP3 extends MovieClip
	{
		public static const DEBUG_URL:String = "http://endo.is-a-geek.com/mxp30/src/"; //use this if you are testing locally
		
		public static var contentHolder:MovieClip = new MovieClip();
		public static var disabler:Sprite = new Sprite();
		public static var currentLocation:String = Word.LOADING;
		public static var wStage:int;
		public static var hStage:int;
		public static var header:Header = new Header();
		public static var footer:Footer = new Footer();
		public static var loadingAni:LoadingAnimation = new LoadingAnimation();
		public static var canvas:BrowserCanvas;
		
		public function MXP3(){
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(event:Event):void {
			//setup the canvas
			canvas = new BrowserCanvas(stage);
			
			//lock the stage to top and left
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, resizeContent);
			
			//setup the stage width and height
			wStage = stage.stageWidth;
			hStage = stage.stageHeight;
			
			//put the header and footer on the stage
			header.init();
			addChild(header);
			
			footer.init();
			addChild(footer);
			
			//setup the content holder
			addChild(contentHolder);
			
			//setup the disabler
			setupDisabler();
			
			//setup the loading animation
			addChild(loadingAni);
			displayLoadingAnimation(true);
			
			//load in the config file and then start loading the forum
			Config.init(loaderInfo.parameters["baseURL"]);
			
			//Make sure this gets called the first time in
			canvas.width = "100%";
			canvas.height = "100%";
			resizeContent();			
		}
				
		public static function addContent(displayObject:DisplayObject):void {
			//make sure the loader is gone
			displayLoadingAnimation(false);
			contentHolder.addChild(displayObject);
		}
		
		public static function getContent(contentName:String):* {
			var contentRequest:* = contentHolder.getChildByName(contentName);
			return contentRequest;
		}
		
		public static function clearBoard():void {
			var numChildren:int = contentHolder.numChildren
			for(var i:uint = 0; i < numChildren; i++){
				var child:DisplayObject = contentHolder.getChildAt(0);
				child = null;
				contentHolder.removeChildAt(0);
			}
			
			//clear the breadcrumbs as well
			BreadCrumbs.clearCrumbs();
			
			//smoke the page nav
			PageNav.clearNav();
			
			//do a quick resize
			//Resizer.resizeContent();
			
			//loading state
			currentLocation = Word.LOADING;
			
			//show the loading animation
			displayLoadingAnimation(true);
		}
		
		public static function enableBoard():void {
			//clear the filters
			contentHolder.filters = [];
			header.filters = [];
			footer.filters = [];
			
			//re-enable the mouse
			header.mouseChildren = true;
			contentHolder.mouseChildren = true;
			footer.mouseChildren = true;
			
			//hide the disabler
			disabler.visible = false;
		}
		
		public static function disableBoard():void {
			//disables the board so you can have things like popups and whatnot and not have users clicking around
			contentHolder.filters = [new BlurFilter()];
			header.filters = [new BlurFilter()];
			footer.filters = [new BlurFilter()];
			
			//make it so that mouse actions are disabled
			header.mouseChildren = false;
			contentHolder.mouseChildren = false;
			footer.mouseChildren = false;
			
			//show the disabler
			disabler.visible = true;
		}
		
		public static function displayLoadingAnimation(isEnabled:Boolean):void {
			if(isEnabled){
				loadingAni.resize();
			}
			
			loadingAni.enabled = isEnabled;
		}
		
		/** STAGE RESIZING EVENT **/
		private function resizeContent(event:Event = null):void {
			//set the width and height and then handle the area where we are at
			MXP3.wStage = stage.stageWidth;
			MXP3.hStage = stage.stageHeight;
			Resizer.resizeContent();
		}
		
		/** BOARD DISABLER **/
		private function setupDisabler():void {
			disabler.graphics.beginFill(0xFFFFFF, .4);
			disabler.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			disabler.graphics.endFill();
			disabler.addEventListener(MouseEvent.ROLL_OVER, disablerOver, false, 0, true);
			disabler.visible = false;
			addChild(disabler);
		}
		
		private function disablerOver(event:MouseEvent):void {
			//if there should be some mouse stuff happening when they rollover the disabler put it in here
		}
		/** END DISABLER **/		
	}
}
